import 'package:flutter/cupertino.dart';
import 'package:hitutor/Widget/MyImage.dart';

class Star extends StatefulWidget {
  double starWidth;
  double diverWidth;
  bool canChoose;
  double value;
  Function(double value)? onChoose;

  Star({this.starWidth = 24, this.diverWidth = 4, this.canChoose = true, this.onChoose, this.value = 5.0});

  @override
  State<StatefulWidget> createState() {
    return _Star();
  }
}

class _Star extends State<Star> {
  setValue(double value) {
    if (widget.canChoose) {
      widget.value = value;
      setState(() {});
      widget.onChoose!(value);
    }
  }

  @override
  Widget build(BuildContext context) {
    double starW = widget.starWidth;
    double diverW = widget.diverWidth;
    String star1 = widget.value > 0 ? (widget.value > 0.5 ? "star_y.png" : "star_yg.png") : "star_g.png";
    String star2 = widget.value > 1 ? (widget.value > 1.5 ? "star_y.png" : "star_yg.png") : "star_g.png";
    String star3 = widget.value > 2 ? (widget.value > 2.5 ? "star_y.png" : "star_yg.png") : "star_g.png";
    String star4 = widget.value > 3 ? (widget.value > 3.5 ? "star_y.png" : "star_yg.png") : "star_g.png";
    String star5 = widget.value > 4 ? (widget.value > 4.5 ? "star_y.png" : "star_yg.png") : "star_g.png";

    return Row(mainAxisSize: MainAxisSize.min, children: [
      LocalImage(star1, width: starW, height: starW, padding: EdgeInsets.all(diverW), onTap: () {
        setValue(1);
      }),
      LocalImage(star2, width: starW, height: starW, padding: EdgeInsets.all(diverW), onTap: () {
        setValue(2);
      }),
      LocalImage(star3, width: starW, height: starW, padding: EdgeInsets.all(diverW), onTap: () {
        setValue(3);
      }),
      LocalImage(star4, width: starW, height: starW, padding: EdgeInsets.all(diverW), onTap: () {
        setValue(4);
      }),
      LocalImage(star5, width: starW, height: starW, padding: EdgeInsets.all(diverW), onTap: () {
        setValue(5);
      })
    ]);
  }
}

class OrgStar extends StatefulWidget {
  Function(int value)? onChoose;

  OrgStar({this.onChoose});

  @override
  State<StatefulWidget> createState() {
    return _OrgStar();
  }
}

class _OrgStar extends State<OrgStar> {
  int value = 5;

  setValue(int value) {
    this.value = value;
    setState(() {});
    widget.onChoose!(value);
  }

  @override
  Widget build(BuildContext context) {
    String star1 = value > 0 ? (value > 0.5 ? "star_org_on.png" : "star_org_half.png") : "star_org_off.png";
    String star2 = value > 1 ? (value > 1.5 ? "star_org_on.png" : "star_org_half.png") : "star_org_off.png";
    String star3 = value > 2 ? (value > 2.5 ? "star_org_on.png" : "star_org_half.png") : "star_org_off.png";
    String star4 = value > 3 ? (value > 3.5 ? "star_org_on.png" : "star_org_half.png") : "star_org_off.png";
    String star5 = value > 4 ? (value > 4.5 ? "star_org_on.png" : "star_org_half.png") : "star_org_off.png";

    return Row(mainAxisSize: MainAxisSize.min, children: [
      LocalImage(star1, width: 20, height: 20, padding: EdgeInsets.symmetric(horizontal: 2), onTap: () {
        setValue(1);
      }),
      LocalImage(star2, width: 20, height: 20, padding: EdgeInsets.symmetric(horizontal: 2), onTap: () {
        setValue(2);
      }),
      LocalImage(star3, width: 20, height: 20, padding: EdgeInsets.symmetric(horizontal: 2), onTap: () {
        setValue(3);
      }),
      LocalImage(star4, width: 20, height: 20, padding: EdgeInsets.symmetric(horizontal: 2), onTap: () {
        setValue(4);
      }),
      LocalImage(star5, width: 20, height: 20, padding: EdgeInsets.symmetric(horizontal: 2), onTap: () {
        setValue(5);
      })
    ]);
  }
}
